<template>
  <div class="login">
    <div class="login-content">
      <el-card style="width: 50%">
        <template #header>
          <div class="card-header">
            <span>登录</span>
          </div>
        </template>
        <div class="loginform">
          <el-form
            ref="ruleFormRef"
            :rules="rules"
            label-width="auto"
            :model="loginform"
            style="width: 300px"
          >
            <el-form-item label="账号" prop="username">
              <el-input v-model="loginform.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="loginform.password" />
            </el-form-item>
          </el-form>
        </div>
        <template #footer>
          <el-button type="primary" @click="submitlogin(ruleFormRef)"
            >登录</el-button
          >
        </template>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from "vue";
import type { ComponentSize, FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { useRouter } from 'vue-router';

import { lutianLogin } from "@/api/user/index.ts";

interface RuleForm {
  username: string;
  password: string;
}
const router = useRouter();
const ruleFormRef = ref<FormInstance>();
const loginform = ref({
  username: "",
  password: "",
});
const rules = reactive<FormRules<RuleForm>>({
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
// const currentUserfun  = inject('currentUserfun')

const submitlogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      //   console.log('submit!')
      lutianLogin(loginform.value).then((res) => {
        console.log(res);
        if (res.code == 200) {
          if (res.msg == "登录成功") {
            ElMessage({
              message: "登录成功",
              type: "success",
              plain: true,
            });
            let currentUserstr = JSON.stringify(res.data);
            // console.log(currentUserstr);
            
            localStorage.setItem('currentUserstr', currentUserstr);
            // router.push({ name: 'login' });
            router.push('/orderform/allorder');
            
          } else {
            ElMessage({
              message: res.msg,
              type: "warning",
              plain: true,
            });
          }
        } else {
          ElMessage({
            message: "登录失败",
            type: "error",
            plain: true,
          });
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>

<style scoped>
/*  */
.login-content {
  height: 100vh;
  display: flex;
  justify-content: center;
  background-color: rgb(225, 234, 233);
  align-items: center;
}

.card-header {
  font-size: 25px;
  font-weight: bold;
}

.loginform {
  display: flex;
  justify-content: center;
}
</style>

<style>
.login-content .el-card__header,
.login-content .el-card__footer {
  border: none !important;
}
</style>